Stops

Downloads + Imports

%run "setup.ipynb"
CPU times: user 57.8 ms, sys: 85 ms, total: 143 ms
Wall time: 3.14 s
Loading BokehJS ...

Read and format data

%time stops_df = pd.read_csv(zipfile.open('stops.txt'))
stops_df.tail()
stops_df.info()
CPU times: user 87.6 ms, sys: 4.27 ms, total: 91.9 ms
Wall time: 91.4 ms
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 41720 entries, 0 to 41719
Data columns (total 11 columns):
 #   Column               Non-Null Count  Dtype  
---  ------               --------------  -----  
 0   stop_id              41720 non-null  object 
 1   stop_code            0 non-null      float64
 2   stop_name            41720 non-null  object 
 3   stop_desc            0 non-null      float64
 4   stop_lat             41720 non-null  float64
 5   stop_lon             41720 non-null  float64
 6   location_type        41720 non-null  int64  
 7   parent_station       28592 non-null  float64
 8   wheelchair_boarding  2946 non-null   float64
 9   platform_code        4416 non-null   object 
 10  zone_id              15337 non-null  object 
dtypes: float64(6), int64(1), object(4)
memory usage: 3.5+ MB
stops_df.fillna('', inplace=True)
stops_df = stops_df.drop(['stop_code', 'stop_desc'], axis=1)
stops_df.loc[stops_df["wheelchair_boarding"] == '','wheelchair_boarding'] = 0
stops_df_multiple_stops = stops_df.copy()
stops_df.drop_duplicates(subset=['stop_name', 'location_type', 'wheelchair_boarding', 'platform_code'],keep='first', inplace = True)
stops_df.head()
stop_id stop_name stop_lat stop_lon location_type parent_station wheelchair_boarding platform_code zone_id
0 000008012713 Rangsdorf, Bahnhof 52.294125 13.431112 0 900000245025.0 0
1 000008010205 Leipzig, Hauptbahnhof 51.344817 12.381321 0 900000550090.0 0
2 000008010327 Senftenberg, Bahnhof 51.526790 14.003977 0 900000435000.0 0
3 000008010324 Schwerin, Hauptbahnhof 53.635261 11.407520 0 900000550112.0 0
4 000008012393 Mühlanger, Bahnhof 51.855704 12.748198 0 900000550319.0 0
stops_df.apply(lambda x: x.unique().size, axis=0)
stop_id                28910
stop_name              13121
stop_lat               13070
stop_lon               13081
location_type              2
parent_station         13090
wheelchair_boarding        2
platform_code             59
zone_id                14590
dtype: int64
# visualization with folium: takes way longer + more memory consumption than bokeh

#f = folium.Figure(width=800, height=600)
#m = folium.Map(location=[45.5236, -122.6750], prefer_canvas=True).add_to(f)
#for lat, lon in zip(stops_df['stop_lat'], stops_df['stop_lon']):
#    folium.CircleMarker(
#        location=[lat, lon],
#        radius=1,
#        color="#3186cc",
#        fill=True,
#        fill_color="#3186cc",
#    ).add_to(m)
#m
def merc_from_arrays(lats, lons):
    r_major = 6378137.000
    x = r_major * np.radians(lons)
    scale = x/lons
    y = 180.0/np.pi * np.log(np.tan(np.pi/4.0 + lats * (np.pi/180.0)/2.0)) * scale
    return (x, y)
p = figure(plot_width=800, plot_height=700,title="Public Transport Stops of VBB",tools="pan,wheel_zoom",
           x_range=(1215654.4978, 1721973.3732), y_range=(6533225.6816, 7296372.9720),
           x_axis_type="mercator", y_axis_type="mercator",
           tooltips=[("Name", "@stop_name"), ("platform", "@platform_code"), ("(Lat, Lon)", "(@stop_lat, @stop_lon)")])
p.add_tile(get_provider(OSM))
stops_df['merc_x'], stops_df['merc_y'] = merc_from_arrays(stops_df['stop_lat'], stops_df['stop_lon'])
p.circle(x='merc_x', y='merc_y', source=stops_df)
show(p)
hv.output(backend="bokeh")
tiles = hv.element.tiles.OSM().opts(alpha=0.5)
stops = hv.Points(stops_df, ['merc_x', 'merc_y'], label='Public Transport Stops')
stops_wa = hv.Points(stops_df.loc[stops_df['wheelchair_boarding'] == 1], ['merc_x', 'merc_y'], label='Wheelchair accessible Stops')
tiles * hd.datashade(stops) + tiles * hd.datashade(stops_wa)

Stations with most stops

stops_df_multiple_stops['stop_name'].value_counts().head(10)
S Potsdam Hauptbahnhof                  24
S Wannsee Bhf (Berlin)                  19
Potsdam, Medienstadt Babelsberg Bhf     19
Cottbus, Hauptbahnhof                   19
Fürstenwalde, Bahnhof                   18
S+U Zoologischer Garten Bhf (Berlin)    17
Potsdam, Johannes-Kepler-Platz          17
S Schöneweide/Sterndamm (Berlin)        16
Frankfurt (Oder), Bahnhof               16
S+U Rathaus Spandau (Berlin)            16
Name: stop_name, dtype: int64
num_stops = stops_df_multiple_stops.groupby(['stop_name']).agg(num_stops=('stop_id', 'count')).query('num_stops > 1').sort_values('num_stops', ascending=False)
num_stops.describe()
num_stops
count 13089.000000
mean 3.184964
std 1.292233
min 2.000000
25% 3.000000
50% 3.000000
75% 3.000000
max 24.000000
num_stops_mean = num_stops['num_stops'].mean()
num_stops_median = num_stops['num_stops'].median()

fig, ax = plt.subplots()
sns.histplot(x='num_stops', data=num_stops, color=sns_c[3], ax=ax, discrete=True)
ax.axvline(x=num_stops_mean, color=sns_c[1], linestyle='--', label=f'mean = {num_stops_mean: ,.2f}')
ax.axvline(x=num_stops_median, color=sns_c[4], linestyle='--',label=f'median = {num_stops_median}')
ax.legend(loc='upper right')
ax.set(title='Number of Stops per Location', xlabel='number of stops', xlim=(0, None))
[Text(0.5, 1.0, 'Number of Stops per Location'),
 Text(0.5, 0, 'number of stops'),
 (0.0, 25.65)]
_images/stops_14_1.png